/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.user;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import queenb.net.entitybean.N550;
import queenb.web.controller.QueenbController;
import queenb.web.store.N550Store;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class N550Controller extends QueenbController {

    private int pn550;
    private String nv551;
    private String nv552;
    private Date nd553;
    private int pn554 = 1;// Phan biêt nha cung cap va khach hang 
    private N550Store n550Store;
    private boolean flagN550;

    public N550Controller() {
        Object pn550i = getRequest().getParameter("pn550");
        if (getRequest().getParameter("action") != null) {
            String actionString = getRequest().getParameter("action");
            if (actionString.equals("addN550")) {
                this.flagN550 = true;
            }
            if (actionString.equals("editN550")) {
                this.flagN550 = false;
                this.pn550 = Integer.parseInt(pn550i.toString());
                getListN550Edit(pn550);
            }
        }
    }
    
    public void getListN550Edit(int idpn550)
    {
        this.pn550 = idpn550;
        try {
            List<N550> listN550Edit = getListN550All();
            if (listN550Edit.size() > 0) {
                for (int i = 0; i < listN550Edit.size(); i++) {
                    if (listN550Edit.get(i).getPn_550() == pn550) {
                        this.nv551 = listN550Edit.get(i).getNv_551();
                        this.nv552 = listN550Edit.get(i).getNv_552();
                        System.out.println("------------>" + listN550Edit.get(i).getNd_553());
                        this.nd553 = listN550Edit.get(i).getNd_553();
                        this.pn554 = listN550Edit.get(i).getPn554();
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Error when getDetailEditN550. ERROR CODE: " + ex.getMessage());
        }
    }

    public List<N550> getListN550All() {
        try {
            n550Store = new N550Store();
            List<N550> listN550 = n550Store.getListN550All(getLogin().getModule(), 99, getLogin().getUserName());
            return listN550;
        } catch (Exception ex) {
            System.out.println("ERORR : " + ex.getMessage());
            return null;
        }
    }

    public void updateN550() throws ParseException {
        n550Store = new N550Store();
        String dateN550 = getRequest().getParameter("dateN550");
        if (!dateN550.equals("")) {
            nd553 = Util.parseDate(dateN550, "dd/MM/yyyy");
        } else {
            nd553 = null;
        }
        if (flagN550 == true) {
            int count = n550Store.updateN550(pn550, nv551, nv552, Util.toSQLDate(this.nd553),pn554, 7, 99, getLogin().getUserName());
            if (count > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }

        } else {
            int count = n550Store.updateN550(pn550, nv551, nv552, Util.toSQLDate(this.nd553),pn554, 7, 99, getLogin().getUserName());
            if (count > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
        }
        sendRedirectUrlTab("setting.jsp?tab=header2&"+Util.convertMD5(getRequest().getRequestURI()));
    }
    
    public int deleteN550(int id)
    {
        n550Store = new N550Store();
         if (id != 0) {
            
            int rows = n550Store.deleteN550(id, getLogin().getUserName());
            return rows;
        } else {
            return 0;
        }
    }

    /**
     * @return the pn550
     */
    public int getPn550() {
        return pn550;
    }

    /**
     * @param pn550 the pn550 to set
     */
    public void setPn550(int pn550) {
        this.pn550 = pn550;
    }

    /**
     * @return the nv551
     */
    public String getNv551() {
        return nv551;
    }

    /**
     * @param nv551 the nv551 to set
     */
    public void setNv551(String nv551) {
        this.nv551 = nv551;
    }

    /**
     * @return the nv552
     */
    public String getNv552() {
        return nv552;
    }

    /**
     * @param nv552 the nv552 to set
     */
    public void setNv552(String nv552) {
        this.nv552 = nv552;
    }

    /**
     * @return the nd553
     */
    public Date getNd553() {
        return nd553;
    }

    /**
     * @param nd553 the nd553 to set
     */
    public void setNd553(Date nd553) {
        this.nd553 = nd553;
    }

    /**
     * @return the flagN550
     */
    public boolean isFlagN550() {
        return flagN550;
    }

    /**
     * @param flagN550 the flagN550 to set
     */
    public void setFlagN550(boolean flagN550) {
        this.flagN550 = flagN550;
    }

    /**
     * @return the pn554
     */
    public int getPn554() {
        return pn554;
    }

    /**
     * @param pn554 the pn554 to set
     */
    public void setPn554(int pn554) {
        this.pn554 = pn554;
    }
}
